rm(list = ls())


# Set Working Directory to Replication Folder
taskdir 		<- "~/Dropbox/final_hai_perlman_replication/"
inputdir 		<- paste0(taskdir, "input/processing/regressions_survey1/")


setwd(taskdir)
library(tidyverse)
library(lfe)
require(data.table)
library(stringi)
library(gtable)
library(gridExtra)
library(grid)
library(lattice)


# Define all DVs
dvs <- c('tax_support' ,'politician_prevent' ,'politician_understand' ,
		'politician_advocate' ,'politician_sympathy'
		,'responsibility_local' ,'responsibility_federal' ,
		'responsibility_international')


# Load regression results
load(paste0(inputdir, 'ols_survey1_ologit.RData'))


toplot <- tibble(dv = NA, subset = NA, beta = NA, se = NA)
for (dv in dvs) {
	temp_regressions <- outlist[[dv]]
	mod1 <- temp_regressions[[1]]
	mod2 <- temp_regressions[[2]]
	mod3 <- temp_regressions[[3]]
	mod4 <- temp_regressions[[4]]

	temp <- tibble(dv = dv, 
				   subset = rep(c('All', 'Republicans', 'Democrats', 'Independents'),1),
				   politician = c(rep('Republican',2),rep('Democrat',2)),
				   beta = c(mod1$coef[1], mod2$coef[1], mod3$coef[1],mod4$coef[1]),
				   se = c( sqrt(vcov(mod1)[1,1]), 
				   		   sqrt(vcov(mod2)[1,1]), 
				   		   sqrt(vcov(mod3)[1,1]),
				   		   sqrt(vcov(mod4)[1,1]))
				   )
	toplot <- bind_rows(toplot, temp)
}
toplot <-toplot%>%filter(!is.na(dv))%>%
				mutate(up = beta + 1.96*se,
					   down = beta-1.96*se,
					   up90 = beta + 1.64*se,
					   down90 = beta - 1.64*se)



### Prep Data for Plotting
# write in DV names
toplot<- toplot %>% mutate(
						question = case_when(
							dv =="politician_understand" ~ "has a good understanding of wildfires and their causes?",
							dv =="politician_sympathy" ~ "How sympathetic or unsympathetic did the politician seem towards those impacted?",
							dv =="politician_prevent" ~ "will work to prevent future wildfires?",
							dv =="politician_advocate" ~ "will be an effective advocate for federal disaster relief?",
							dv =="responsibility_international" ~ "The international community",
							dv =="responsibility_federal" ~ "The federal government",
							dv =="responsibility_local" ~ "Local/state government",
							dv =="tax_support" ~ "How likely would you be to support this new tax?",
							TRUE ~ as.character(NA)
						)
)

toplot$question <- factor(toplot$question,
						levels = c("has a good understanding of wildfires and their causes?",
								   "will work to prevent future wildfires?",
								   "will be an effective advocate for federal disaster relief?",
								   "How sympathetic or unsympathetic did the politician seem towards those impacted?",
								   "How likely would you be to support this new tax?",
								   "Local/state government",
								   "The federal government",
								   "The international community"))

# get party variable in correct order
# get party variable in correct order
toplot$subset<- factor(toplot$subset,
											levels = c("Independents","Democrats",
																"Republicans","All"))


#----Plot Results by Question
# Main
q1<- ggplot(toplot %>% filter(dv %in%
											 c('politician_understand',
											   'politician_prevent',
											   'politician_advocate')) )+
	geom_vline(xintercept = 0, linetype = 'dashed')+
	geom_linerange(aes(y = subset, xmax = up90, xmin = down90,
								color = subset,
								linetype = "90% CI"), 
								size = 0.6, width = 0.2)+
	geom_linerange(aes(y = subset, xmax = up, xmin = down,
								color = subset,
								linetype = "95% CI"), 
								size = 0.6, width = 0.2)+
	scale_linetype_manual(name = 'CI',
						  values = c('solid','dotted'))+
	geom_point(aes(y = subset, x = beta,
								color = subset), size = 2)+
	geom_text(aes(y = subset, x = beta,
												color = subset, label = round(beta,2) ),
												size = 3,
											nudge_y = 0.3)+
	theme_bw()+
	facet_wrap(~question,ncol=1)+
	scale_color_manual(values = list('All' = 'black',
																	'Democrats' = 'steelblue3',
																  'Independents' = 'grey60',
																  'Republicans' = 'tomato3'))+
	ggtitle("How confident are you that the politician:")+
	xlab('Treatment Effect (log-odds)')


# Sympathy
q2<- ggplot(toplot %>% filter(dv %in%
											 c('politician_sympathy')) )+
	geom_vline(xintercept = 0, linetype = 'dashed')+
	geom_linerange(aes(y = subset, xmax = up90, xmin = down90,
								color = subset,
								linetype = "90% CI"), 
								size = 0.6, width = 0.2)+
	geom_linerange(aes(y = subset, xmax = up, xmin = down,
								color = subset,
								linetype = "95% CI"), 
								size = 0.6, width = 0.2)+
	scale_linetype_manual(name = 'CI',
						  values = c('solid','dotted'))+
	geom_point(aes(y = subset, x = beta,
								color = subset), size = 2)+
	geom_text(aes(y = subset, x = beta,
												color = subset, label = round(beta,2) ),
												size = 3,
											nudge_y = 0.3)+
	theme_bw()+
	scale_color_manual(values = list('All' = 'black',
																	'Democrats' = 'steelblue3',
																  'Independents' = 'grey60',
																  'Republicans' = 'tomato3'))+
	ggtitle("How sympathetic or unsympathetic did the\n politician seem towards those impacted?")+
	xlab('Treatment Effect (log-odds)')


# Tax support
q3<- ggplot(toplot %>% filter(dv %in%
											 c('tax_support')) )+
	geom_vline(xintercept = 0, linetype = 'dashed')+
	geom_linerange(aes(y = subset, xmax = up90, xmin = down90,
								color = subset,
								linetype = "90% CI"), 
								size = 0.6, width = 0.2)+
	geom_linerange(aes(y = subset, xmax = up, xmin = down,
								color = subset,
								linetype = "95% CI"), 
								size = 0.6, width = 0.2)+
	scale_linetype_manual(name = 'CI',
						  values = c('solid','dotted'))+
	geom_point(aes(y = subset, x = beta,
								color = subset), size = 2)+
	geom_text(aes(y = subset, x = beta,
												color = subset, label = round(beta,2) ),
												size = 3,
											nudge_y = 0.3)+
	theme_bw()+
	facet_wrap(~question,ncol=1)+
	scale_color_manual(values = list('All' = 'black',
																	'Democrats' = 'steelblue3',
																  'Independents' = 'grey60',
																  'Republicans' = 'tomato3'))+
	ggtitle("The government is considering imposing an energy tax \nto protect against future wildfires and other natural \ndisasters. This tax is projected to increase the average American \nhousehold's energy bill by 10-20%. How likely \nwould you be to support this new tax?")+
	xlab('Treatment Effect (log-odds)')


#save
ggsave(paste0(taskdir, 'output/regression_plots/ologit_survey1_controls/survey1_ologit_main.png'),
			q1,
			width = 7,
			height = 6,
				dpi = 600)

ggsave(paste0(taskdir, 'output/regression_plots/ologit_survey1_controls/survey1_ologit_sympathy.png'),
			q2,
			width = 7,
			height = 2.66,
				dpi = 600)
#2.66
ggsave(paste0(taskdir, 'output/regression_plots/ologit_survey1_controls/survey1_ologit_taxsupport.png'),
			q3,
			width = 7,
			height = 6,
				dpi = 600)














